package com.chen.authority.controller;

import com.chen.authority.common.annotation.Log;
import com.chen.authority.common.enums.BusinessType;
import com.chen.authority.common.response.MultiResponse;
import com.chen.authority.common.response.Response;
import com.chen.authority.common.response.SingleResponse;
import com.chen.authority.model.dto.MenuDto;
import com.chen.authority.model.qo.MenuQo;
import com.chen.authority.model.vo.MenuVo;
import com.chen.authority.service.MenuService;
import lombok.RequiredArgsConstructor;

import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import javax.validation.Valid;

/**
 * 菜单管理
 *
 * @author chengy
 * @date 2022/11/24
 */
@Api(tags = "菜单管理 MenuController")
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/menu")
public class MenuController {

    private final MenuService menuService;

    @ApiOperation("新增菜单")
    @SaCheckPermission("system:menu:add")
    @Log(module = "系统管理-菜单管理", businessType = BusinessType.INSERT)
    @PostMapping
    public Response add(@Valid @RequestBody MenuDto dto) {
        return menuService.addOrUpdateMenu(null, dto);
    }

    @ApiOperation("删除菜单")
    @SaCheckPermission("system:menu:delete")
    @Log(module = "系统管理-菜单管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public Response delete(@PathVariable Integer id) {
        return menuService.deleteMenuById(id);
    }

    @ApiOperation("更新菜单")
    @SaCheckPermission("system:menu:update")
    @Log(module = "系统管理-菜单管理", businessType = BusinessType.UPDATE)
    @PutMapping("/{id}")
    public Response update(@PathVariable Integer id, @Valid @RequestBody MenuDto dto) {
        return menuService.addOrUpdateMenu(id, dto);
    }

    @ApiOperation("获取菜单详细信息")
    @SaCheckPermission("system:menu:update")
    @GetMapping("/{id}")
    public SingleResponse<MenuVo> get(@PathVariable Integer id) {
        return SingleResponse.success(menuService.getById(id).toVo(MenuVo.class));
    }

    @ApiOperation("查询菜单列表")
    @SaCheckPermission("system:menu:list")
    @GetMapping("/list")
    public MultiResponse<MenuVo> list(MenuQo qo) {
        return MultiResponse.success(menuService.findMenusByCondition(qo));
    }

}
